<?php
	include("connect.php");
	$css = '<link rel="stylesheet" type="text/css" href="asset/css/class_admin.css" />';
	$css .= '<link rel="stylesheet" type="text/css" href="asset/css/niceforms-default.css" />';
	$css .= '<script language="javascript" type="text/javascript" src="asset/js/niceforms.js"></script>';
	$css .= '<script language="javascript" type="text/javascript" src="asset/js/datetimepicker.js"></script>';
	$css .= '<link rel="stylesheet" type="text/css" media="all" href="asset/css/paging.css">';
	$title = 'Admin List Book';
	$page_name = 'admin_requested_report';
	$page = 1;

$sql = "select user_category from m_user where user_id=".$_SESSION['userid'];
$retval = mysql_query( $sql, $con );
while($row = mysql_fetch_array($retval))
	{
		$user_category = $row[0];
	}
if(isset($_SESSION["userid"]))
	{
		if($user_category ==9)
		{
			include("header_loggedinadmin.php");
		}else
		{
			include("header_loggedin.php");
		}
	}
else
{
	include("header.php");
}
//check category

if($user_category !=9)
{
	echo '<script>';
		echo "alert('You can not access this page!');";
		echo 'window.location = "index.php"';
		echo '</script>';
}	

//paging

	$targetpage = $page_name.".php"; 	
	$limit = 9; 
$sql = "select count(*) from (SELECT COUNT(t_request_book.book_id) AS number_of_book, m_book.book_title, m_book.ISBN, m_book.book_author, t_category.category_name FROM t_request_book";
						$sql .= " INNER JOIN m_book ON t_request_book.book_id = m_book.book_id";
						$sql .= " INNER JOIN t_book_category ON t_request_book.book_id = t_book_category.book_id";
						$sql .= " INNER JOIN t_detail_category ON t_book_category.category_detail_id = t_detail_category.category_detail_id";
						$sql .= " INNER JOIN t_category ON t_detail_category.category_id = t_category.category_id";
						$sql .= " WHERE t_request_book.is_get = 1";
						if(isset($_REQUEST['book_name']))
						{
							$bookname = $_REQUEST['book_name'];
							$sql .= " AND m_book.book_title LIKE '%" .$bookname ."%'";
						}
						$sql .= " GROUP BY t_request_book.book_id";
						$sql .= " ORDER BY COUNT(t_request_book.book_id) DESC) Z";
						//echo $sql;
						if (!mysql_query($sql,$con))
						{
							echo $sql;
							die('<br><br>Query execution error: ' . mysql_error());
						}
						$result = mysql_query($sql);
    while($row = mysql_fetch_array($result))
	{
		$total_pages =$row[0];
	}
	
	
	//$total_pages = mysql_fetch_array(mysql_query($query));
	//$total_pages = $total_pages[num];
	$stages = 3;
	$page = mysql_escape_string($_GET['page']);
	if($page){
		$start = ($page - 1) * $limit; 
	}else{
		$start = 0;	
		}
		
	$sql = "SELECT COUNT(t_request_book.book_id) AS number_of_book, m_book.book_title, m_book.ISBN, m_book.book_author, t_category.category_name,request_book_id FROM t_request_book";
						$sql .= " INNER JOIN m_book ON t_request_book.book_id = m_book.book_id";
						$sql .= " INNER JOIN t_book_category ON t_request_book.book_id = t_book_category.book_id";
						$sql .= " INNER JOIN t_detail_category ON t_book_category.category_detail_id = t_detail_category.category_detail_id";
						$sql .= " INNER JOIN t_category ON t_detail_category.category_id = t_category.category_id";
						$sql .= " WHERE t_request_book.is_get = 1";
						if(isset($_REQUEST['book_name']))
						{
							$bookname = $_REQUEST['book_name'];
							$sql .= " AND m_book.book_title LIKE '%" .$bookname ."%'";
							$pagingadd .= '&book_name='.$book_name;
						}
						$sql .= " GROUP BY t_request_book.book_id";
						$sql .= " ORDER BY COUNT(t_request_book.book_id) DESC";
						$sql .= " LIMIT $start, $limit";
					
	$result = mysql_query($sql);
	
	// Initial page num setup
	if ($page == 0){$page = 1;}
	$prev = $page - 1;	
	$next = $page + 1;							
	$lastpage = ceil($total_pages/$limit);		
	$LastPagem1 = $lastpage - 1;	
	
	$paginate = '';
	include("paging.php");


?>

<div id="blue_bg">
	<div id="main">
        <div class="wrapper">
        	<div id="content">
            	<div id="white_box">
                	<h3 class="flag_long">Sold Report</h3>
                    <p>
                    	<form name="show">
                    	<input type="text" name="book_name" size="50" /> &nbsp;<input type="submit" value="SEARCH" id="show_btn" />
                        </form>
                    </p>
                    <table id="rounded-corner" summary="List Book BookSquare">
                        <thead>
                            <tr>
                                <th scope="col" class="rounded-company"></th>
                                <th scope="col" class="rounded">Number of Book</th>
                                <th scope="col" class="rounded">Book Name</th>
                                <th scope="col" class="rounded">ISBN</th>
                                <th scope="col" class="rounded">Author</th>
                                <th scope="col" class="rounded">Category</th>
                                <th scope="col" class="rounded-q4">Detail</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php
						
						while($row = mysql_fetch_array($result))
						{
							echo '
                            <tr>
                                <td><input type="checkbox" name="" /></td>
                                <td>'.$row['number_of_book'].'</td>
                                <td>'.$row['book_title'].'</td>
                                <td>'.$row['ISBN'].'</td>
                                <td>'.$row['book_author'].'</td>
                                <td>'.$row['category_name'].'</td>
                                <td><a href="request_book_detail.php?requestbookid='.$row['request_book_id'].'" class="ask">detail</a></td>
                            </tr>
							';
						}
                        ?>
                            
                        </tbody>
                    </table>
                    
                    
                    <div class="pagination">
                   <?php echo $paginate; ?>
                    </div> 
                    <div class="clear"></div>
           		</div>
            </div>
        </div>
    </div>
</div>

<?php
 include("footer.php");
?>